package cn.com.zcode.core.control;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PerformanceInterceptor implements Interceptor, MethodInterceptor {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(PerformanceInterceptor.class);

	@Override
	public void destroy() {
		System.out.println("注消性能拦截器。。。");

	}

	@Override
	public void init() {
		System.out.println("初始化性能拦截器。。。");

	}

	@Override
	public String intercept(ActionInvocation action) throws Exception {
		long time = System.currentTimeMillis();
		String result = action.invoke();
		logger.info("(V02)调用耗时:" + (System.currentTimeMillis() - time) + " ms");
		return result;
	}

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		Method method = invocation.getMethod();
		long time = System.currentTimeMillis();
		Object returnValue = invocation.proceed();
		logger.info("(V02)调用[" + method.getName() + "]耗时:" + (System.currentTimeMillis() - time) + " ms");

		return returnValue;

	}

}
